import { useEffect } from "react";
import { useLocation, useNavigate } from "react-router-dom";

export default function useLoginState(root:string,path:string) {
  const navigate = useNavigate();
  const { pathname } = useLocation();
  // 直接在这里进行token的判断
  let token = localStorage.getItem("token");
  useEffect(() => {
    if (token) {
      if (pathname === root) {
        navigate(path);
      }
    } else {
      navigate("/login");
    }
  });
}